package com.atom.common.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import com.atom.common.entity.RunningTaskDO;
import com.atom.common.util.PreConditionUtil;

@Repository
public class RunningTaskDao extends AvensCommonDao<RunningTaskDO> {

  public void delete(String taskId, String assignee) {
    PreConditionUtil.checkNotEmpty(taskId, "taskId cannot be empty");
    PreConditionUtil.checkNotEmpty(assignee, "assignee cannot be empty");

    final String sql = "DELETE FROM atm_common_ru_task "
        + "WHERE TASK_ID = '"
        + taskId
        + "' AND ASSIGNEE = '"
        + assignee
        + "'";
    super.executeSQL(sql);
  }

  public void updateTaskName(String taskId, String taskName) {
    final String sql = "UPDATE atm_common_ru_task "
        + "SET PROCESS_INS_NAME = '"
        + taskName
        + "' WHERE TASK_ID = '"
        + taskId
        + "' ";
    super.executeSQL(sql);
  }

  public void deleteClaimedTask(String taskId, String assignee, String owner) {
    PreConditionUtil.checkNotEmpty(taskId, "taskId cannot be empty");
    PreConditionUtil.checkNotEmpty(assignee, "assignee cannot be empty");

    final String sql = "DELETE FROM atm_common_ru_task "
        + "WHERE TASK_ID = '"
        + taskId
        + "' AND (ASSIGNEE != '"
        + assignee
        + "' OR OWNER != '"
        + owner
        + "')";
    super.executeSQL(sql);
  }

  public void updateLock(String taskId, String assignee, int lock) {
    PreConditionUtil.checkNotEmpty(taskId, "taskId cannot be empty");
    PreConditionUtil.checkNotEmpty(assignee, "assignee cannot be empty");

    final String sql = "UPDATE atm_common_ru_task "
        + "SET IS_LOCK = "
        + lock
        + " WHERE TASK_ID = '"
        + taskId
        + "' AND ASSIGNEE = '"
        + assignee
        + "'";
    super.executeSQL(sql);
  }

  public void addCoop(String taskId, String assignee, String owner) {
    List<RunningTaskDO> taskList = findByTaskIdByAssigneeByOwner(taskId, owner, owner);
    for (RunningTaskDO task : taskList) {
      if (!task.isLock()) {
        RunningTaskDO coop = new RunningTaskDO();
        BeanUtils.copyProperties(task, coop, "id");
        coop.setAssignee(assignee);
        coop.setOwner(owner);
        super.save(coop);
      }
    }
  }

  private List<RunningTaskDO> findByTaskIdByAssigneeByOwner(String taskId, String assignee,
      String owner) {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("taskId", taskId);
    properties.put("assignee", assignee);
    properties.put("owner", owner);
    return super.findByProperties(properties);
  }

  public void addOut(String taskId, String assignee, String owner) {
    List<RunningTaskDO> taskList = findByTaskIdByAssigneeByOwner(taskId, owner, owner);
    for (RunningTaskDO task : taskList) {
      task.setAssignee(assignee);
      task.setOwner(owner);
      super.update(task);
    }

  }

  public void delOut(String taskId, String assignee, String owner) {
    List<RunningTaskDO> taskList = findByTaskIdByAssigneeByOwner(taskId, assignee, owner);
    for (RunningTaskDO task : taskList) {
      task.setAssignee(owner);
      task.setOwner(owner);
      super.update(task);
    }    
  }

  public void delCoop(String taskId, String assignee, String owner) {
    List<RunningTaskDO> taskList = findByTaskIdByAssigneeByOwner(taskId, assignee, owner);
    for (RunningTaskDO task : taskList) {
      if (task.isLock()) {
        task.setAssignee(owner);
        task.setOwner(owner);
        super.update(task);
      } else {
        super.delete(task);
      }
    } 
  }
  
  public List<RunningTaskDO> getListByTaskId(String taskid) {
	    PreConditionUtil.checkNotEmpty(taskid, "taskId cannot be empty");

	    return super.findByProperty("taskId", taskid);
	  }

}
